#include "StdAfx.h"
#include "SearchManager.h"

SearchManager::SearchManager(std::string& filename, DocumentManager& dm, TermManager& tm)
	:m_File(filename, std::ios::in | std::ios::binary), m_DocumentManager(dm), m_TermManager(tm), m_pTermData(0)
{

}

SearchManager::~SearchManager()
{
	m_File.close();
}

IndexData* SearchManager::FindIndexData(std::string name)
{
	TermData& termData = m_TermManager.FindTermData(name);
	if(termData.index == 0)
	{
		m_pTermData = 0;
		m_TermCursor = 0;
		return 0;
	}
	
	m_pTermData = &termData;
	m_TermCursor = 0;
	m_File.seekg(m_pTermData->begin * 12);
	return NextIndexData();
}

IndexData* SearchManager::FindIndexData(std::string name, TermData & td)
{
	TermData& termData = m_TermManager.FindTermData(name);
	if(termData.index == 0)
	{
		m_pTermData = 0;
		m_TermCursor = 0;
		return 0;
	}
	
	m_pTermData = &termData;
	m_TermCursor = 0;
	td = termData;
	m_File.seekg(m_pTermData->begin * 12);
	return NextIndexData();
}

IndexData* SearchManager::NextIndexData()
{
	if(m_pTermData == 0)
		return 0;

	if(m_TermCursor >= m_pTermData->df)
	{
		m_pTermData = 0;
		return 0;
	}

	//m_File.seekg(12 * (m_pTermData->begin + (m_TermCursor++)));
	m_File.read((char*)&m_TempIndexData.documentIndex, sizeof(unsigned int));
	m_File.read((char*)&m_TempIndexData.tf, sizeof(unsigned int));
	m_File.read((char*)&m_TempIndexData.weight, sizeof(float));
	m_TermCursor++;
	return &m_TempIndexData;
}